// pages/boss/apply/apply.js
const app = getApp()
const util = require("../../../utils/util.js")
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: '0',
    tablineStyle: '',
    list: [],

    formShow: false,
    form:{
      name:'',
      commission:90
    }
  },
  // 切换tab
  tabChoose: function (e) {
    var that = this;
    var cur = e.target.dataset.current;
    var type = cur == '0' ? 'agent' : 'shop';
    if (that.data.currentTab == cur) { return false; }
    else {
      that.setData({
        currentTab: cur,
        pagenum: 1,
        list: []
      })
    }
    that.tabLine();
    that.review(type);

  },
  // 切换tab滑块动画
  tabLine: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    var currentTab = that.data.currentTab;
    query.select('.tab').boundingClientRect(function (rect) {
      that.setData({
        tablineStyle: "width:" + rect.width / 2 + "px;transform: translateX(" + rect.width * currentTab / 2 + "px);"
      })
    }).exec();
  },
  // 获取列表
  review: function (type) {
    var that = this;
    var data = {
      type: type
    };
    app.post('identity/showApply', data).then((res) => {
      if (res.code == 200) {
        that.setData({
          list: res.list
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  // 通过或否决
  reviewTap: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var type = e.currentTarget.dataset.type;
    var data = {
      id: id,
      commission:'',
      name:name,
      type: type
    };
    that.setData({
      ['form.id']: id,
      ['form.commission']: 90,
      ['form.name']: name,
      ['form.type']: type
    })
    if (type =='refuse'){
      wx.showModal({
        title: '提示',
        content: '确定否决该申请吗？',
        success(res){
          if(res.confirm){
            that.processApply(data);
          }
        }
      })
    }else{
      that.setData({ formShow: true });
    }
  },
  applyConfirm:function(){
    var that = this;
    var data = {
      id: that.data.form.id,
      commission: that.data.form.commission,
      name: that.data.form.name,
      type: that.data.form.type
    };
    wx.showModal({
      title: '提示',
      content: '确定同意该申请吗？',
      success(res) {
        if (res.confirm) {
          that.processApply(data);
        }
      }
    })
  },
  processApply: function (data) {
    app.post('identity/processApply', data).then((res) => {
      console.log(res)
      if (res.code == 200) {
        Toast.success(res.message);
        var type = this.data.currentTab == '0' ? 'agent' : 'shop';
        this.review(type);
      }else{
        Toast.fail(res.message);
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  applyClose() {
    this.setData({ formShow: false });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = this.data.currentTab == '0' ? 'agent' : 'shop';
    this.tabLine();
    this.review(type);
  },
  nameChange: function (event) {
    this.setData({
      ['form.name']: event.detail
    })
  },
  rateChange: function (event) {
    this.setData({
      ['form.commission']: parseInt(event.detail)
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var type = this.data.currentTab == '0' ? 'agent' : 'shop';
    this.setData({
      list: []
    })
    this.review(type);
  }

})